import React, { PureComponent } from 'react';
import { Card, Table, message, Modal } from 'antd'
import { PageContainer } from '@ant-design/pro-layout';
import { getExamList, getExamListScore } from '@/services/studentExam'
import { getHandle } from '@/services/baseurl'
import { simulationPoints } from '@/services/setting'
import { dev, checkHost,isDevelopment } from '@/utils/utils'
import { submitExam } from '@/services/studentExam'
import { history } from 'umi'
class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props)
        const userinfo = JSON.parse(localStorage.getItem('userinfo') || '{}');
        const hasTwo:any = localStorage.getItem('hasTwo')||2;
        const openid = userinfo.openid;
        if (checkHost()) {
            if (!openid&&hasTwo!=1) {
                Modal.confirm({
                    title: <p style={{ fontSize: 20, marginTop: -3 }}>操作提醒</p>,
                    content: <p style={{ fontSize: 18 }}>新用户请绑定您的微信账号解锁培训练习权限，绑定成功后每次登录平台请使用微信进行扫码登录即可。</p>,
                    okText: '去绑定',
                    style: { fontSize: 30, color: "#f60" },
                    onOk: () => {
                        location.href = `/index/index/login_weixin`;
                    },
                });
                history.goBack();
            }
        }

        this.state = {
            data: [],
            loading: true
        }
    }
    goToExam = (id: string, row: any) => () => {
        const uid = localStorage.getItem('id') || '';
        this.setState({
            loading: true
        })
        simulationPoints(uid).then(res => {
            if (res.status === 1) {
                const operation = `进入了模拟考试（${row.name}）`
                getHandle({ operation })
                submitExam({
                    duration: '', pid: id, uid, grade: '0', total: '0', correct_total: '0', did_total: '0',
                    topic: [[], [], []], answer: [[], [], []], id: '', wrong: '', label: ''
                }).then(response => {
                    this.setState({
                        loading: false
                    })
                    const topic_id = response.id;
                    const { showAnswer } = this.props.match.params;
                    localStorage.setItem('exam_page',location.hash.replace('#',''))
                    this.props.history.push(`/examPage/${id}/${row.type}/${topic_id}/${showAnswer}`)
                    // window.open(dev + `#/examPage/${id}/${row.type}/${topic_id}/${showAnswer}`, '考试中心', 'width=' + (window.screen.availWidth - 10) + ',height=' + (window.screen.availHeight - 20) + ',top=0,left=0,resizable=no,status=yes,menubar=no,scrollbars=yes');
                })
            } else {
                message.error('积分不足')
            }
        })

    }
    goToCheck = (id: string, row: any,wong:number) => () => {
        if (!row.score) return message.error('您未考试，不能查卷')
        localStorage.setItem('exam_page',location.hash.replace('#',''))
        this.props.history.push('/checkPaper/' + id+'?wrong='+wong)
    }
    componentDidMount() {
        const uid = localStorage.getItem('id') || '';
        const { subject } = this.props.match.params;
       const {type} = this.props.location.query;
        getExamList({ uid, subject, type}).then(res => {
            this.setState({
                loading: false
            })
            if (!res || !res[0]) return;
            let arr = [];
            for (let i = 0; i < res.length; i++) {
                const item = res[i];
                arr.push(item.id)
            }
            getExamListScore({ uid, id: arr }).then(response => {
                for (let i = 0; i < res.length; i++) {
                    const item = res[i];
                    item.score = response[i].score;
                }
                this.setState({
                    data: res,

                })
            })

        })
    }

    render() {
        const { data, loading } = this.state;
        const columns = [
            {
                title: '考试名称',
                dataIndex: 'name'
            },
            {
                title: '所属科目',
                dataIndex: 'subjects'
            },
            {
                title: '时长',
                dataIndex: 'test_time',
                render: (test_time: number) => {
                    return test_time + '分钟'
                }
            },
            {
                title: '总分',
                dataIndex: 'total'
            },
            {
                title: '得分',
                dataIndex: 'score',
                render: (score: number) => {
                    return score || '0'
                }
            },
            {
                title: '进入考场',
                dataIndex: 'id',
                render: (id: string, row: any) => {
                    return <a onClick={this.goToExam(id, row)}>进入考场</a>
                }
            },
            {
                title: '查卷',
                dataIndex: 'id',
                render: (id: string, row: any) => {
                    return <React.Fragment>
                         <a onClick={this.goToCheck(id, row,0)}>查卷</a>
                         <a style={{marginLeft:20}} onClick={this.goToCheck(id, row,1)}>查看错题</a>
                    </React.Fragment>
                }
            }
        ]
        // if(isDevelopment()){
        //     columns.splice(5,1)
        // }
        return (
            <PageContainer title="模拟考试">
                <Card><Table loading={loading} columns={columns} dataSource={data} rowKey="id" /></Card>
            </PageContainer>
        );
    }
}

export default Index;
